package v1

import (
	"errors"
	"github.com/gin-gonic/gin"
	"mall_2.0/consts"
	"mall_2.0/pkg/e"
	"mall_2.0/pkg/utils/ctl"
	"mall_2.0/pkg/utils/log"
	"mall_2.0/service"
	"mall_2.0/types"
	"net/http"
)

// UserRegisterHandler 用户注册
func UserRegisterHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.UserRegisterReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
		}

		// 参数校验
		if req.Key == "" || len(req.Key) != consts.EncryptMoneyKeyLength {
			err := errors.New("key 长度错误，必须是6位数")
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}

		l := service.GetUserSrv()
		resp, err := l.RegisterUser(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// UserLoginHandler 用户登录
func UserLoginHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.UserLoginReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
		}
		l := service.GetUserSrv()
		resp, err := l.LoginUser(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// UserUpdateHandler 更新用户信息
func UserUpdateHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.UserInfoUpdateReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}

		l := service.GetUserSrv()
		resp, err := l.UpdateUserInfo(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// ShowUserInfoHandler 获取用户信息
func ShowUserInfoHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.UserInfoShowReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetUserSrv()
		resp, err := l.ShowUserInfo(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// SendEmailHandler 发送邮件
func SendEmailHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.SendEmailServiceReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetUserSrv()
		resp, err := l.SendEmail(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// ValidEmailHandler 验证邮件
func ValidEmailHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ValidEmailServiceReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetUserSrv()
		resp, err := l.Valid(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// UserFollowingHandler 关注用户
func UserFollowingHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.UserFollowingReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetUserSrv()
		resp, err := l.UserFollow(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// UserUnFollowingHandler 取关用户
func UserUnFollowingHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.UserUnFollowingReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetUserSrv()
		resp, err := l.UserUnFollow(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// UploadAvatarHandler 上传头像
func UploadAvatarHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.UserServiceReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		file, fileHeader, _ := c.Request.FormFile("file")
		if fileHeader == nil {
			err := errors.New(e.GetMsg(e.ErrorUploadFile))
			c.JSON(consts.IlleageRequest, ErrorResponse(c, err))
			log.LogrusObj.Infoln(err)
			return
		}
		fileSize := fileHeader.Size

		l := service.GetUserSrv()
		resp, err := l.UserAvatarUpload(c.Request.Context(), file, fileSize, &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}
